package web

import (
	"TD-UI/internal/consts"
	"TD-UI/internal/model"
	"TD-UI/internal/model/entity"
	"TD-UI/internal/service"
	"fmt"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
)

type Controller struct{}

func (c *Controller) Tpl(req *ghttp.Request) {
	data := g.Map{
		"name":   "王也道长",
		"lesson": "GoFrame入门课程",
		"num":    5,
		"what":   "模板引擎使用示例",
	}

	req.Response.WriteTpl("hello/index.html", data)
}

func (c *Controller) Index(req *ghttp.Request) {
	result, _ := service.Link().GetAll(req.Context(), model.LinkGetReq{})
	r := result.Result.([]entity.Link)
	res := make([]entity.Link, 0)
	for _, v := range r {
		v.Password = ""
		v.User = ""
		v.Host = ""
		v.Port = 0
		res = append(res, v)
	}
	data := g.Map{
		"link": res,
	}
	req.Response.WriteTpl("web/index.html", data)
}

func (c *Controller) Db(req *ghttp.Request) {
	linkId := req.Get("linkId")
	fmt.Println("-----", req.Get("linkId"))
	result, err := service.Link().Link(req.Context(), model.LinkReq{Linkid: linkId.String()})
	var r = make([]string, 0)
	linkSuccess := true
	if err != nil {
		linkSuccess = false
	} else {
		r = result.Result.([]string)
	}

	data := g.Map{
		"linkDb":      r,
		"linkId":      linkId,
		"linkSuccess": linkSuccess,
	}
	req.Response.WriteTpl("web/db.html", data)
}

func (c *Controller) Table(req *ghttp.Request) {
	linkId := req.Get("linkId")
	dbName := req.Get("dbName")
	sp := req.Get("sp")
	fmt.Println("-----", req.Get("linkId"))
	fmt.Println("-----", req.Get("dbName"))
	reqModel := model.TableReq{
		LinkId:      linkId.String(),
		DbName:      dbName.String(),
		SupperTable: sp.Bool(),
	}
	result, _ := service.Table().GetTable(req.Context(), reqModel)
	r := result.Result.([]string)
	data := g.Map{
		"table":  r,
		"sp":     sp.Bool(),
		"LinkId": linkId,
		"DbName": dbName,
	}
	req.Response.WriteTpl("web/table.html", data)
}

func (c *Controller) List(req *ghttp.Request) {
	linkId := req.Get("linkId")
	dbName := req.Get("dbName")
	tableName := req.Get("tableName")
	stableName := req.Get("stableName")
	reqModel := model.TableDataReq{
		LinkId:     linkId.String(),
		DbName:     dbName.String(),
		TableName:  tableName.String(),
		StableName: stableName.String(),
	}
	result, _ := service.Table().GetTableColumns(req.Context(), reqModel)
	r := result.TableInfo
	data := g.Map{
		"columns":    r,
		"LinkId":     linkId,
		"DbName":     dbName,
		"TableName":  tableName,
		"StableName": stableName,
	}
	req.Response.WriteTpl("web/tableData.html", data)
}

func (c *Controller) Query(req *ghttp.Request) {
	linkId := req.Get("linkId")
	dbName := req.Get("dbName")
	data := g.Map{
		"LinkId": linkId,
		"DbName": dbName,
	}
	req.Response.WriteTpl("web/tableQuery.html", data)
}

func (c *Controller) Createdb(req *ghttp.Request) {
	linkId := req.Get("linkId")
	data := g.Map{
		"LinkId": linkId,
	}
	req.Response.WriteTpl("web/createDb.html", data)
}

func (c *Controller) Createtable(req *ghttp.Request) {
	linkId := req.Get("linkId")
	dbName := req.Get("dbName")
	data := g.Map{
		"LinkId":    linkId,
		"DbName":    dbName,
		"FieldType": consts.TableFiledDefaultLen,
	}
	req.Response.WriteTpl("web/createTable.html", data)
}

func (c *Controller) Execsql(req *ghttp.Request) {
	linkId := req.Get("linkId")
	dbName := req.Get("dbName")
	data := g.Map{
		"LinkId": linkId,
		"DbName": dbName,
	}
	req.Response.WriteTpl("web/tableExec.html", data)
}
